1.6 - C# Methods


What are methods?

Methods are basically functions, containers of code which we can call (execute) whenever we need.

To declare a method we need to follow a certain syntax which is the following:

  • The method accessibility: can either be public, private, protected or internal (there may be others but we aren't interested for these mods)
  • The return type of the method, because every method can return a value of the defined variable type
  • The method name, which we use to call (execute) it
  • [optional] The method parameter/s


// declaring a method of public accessibility, with a return type of void and "MyMethod" as name
public void MyMethod()
{

}
            

Methods accessibility types

As said before we have mainly four accessibility types which are public, private, protected and internal.
We will talk about accessibility in the classes lesson since we need to understand something more to get it.

Methods return type

Each method can, and you hear it right, can, not must, return a value of the defined type.
In the example below "MyMethod" return an int value which is equal to 5;


// declaring a method of public accessibility, with a return type of int and "MyMethod" as name
public int MyMethod()
{
    return 5; // the int value the method returns
}
            

Other examples


// declaring a method of public accessibility, with a return type of bool and "MyMethod" as name            
public bool MyMethod()
{
    return true; // returning a boolean
}
            

// declaring a method of public accessibility, with a return type of string and "MyMethod" as name            
public string MyMethod()
{
    return "Hello"; // returning a string
}
            

As said before, a method can and not must return a value. This means we have a special type which is called void.
A method with a return type of void simply means that it returns nothing and it just executes the code inside the curly braces.


// declaring a method of public accessibility, with a return type of void and "MyMethod" as name            
public void MyMethod()
{
    // do the stuff in here
}
            

On the other side, if we define a return type which is not void, we must use the return statement with the appropriate value type.

How to call a method?

Calling a method is very simple.
To do it we need to write the method name, followed by (), inside another method.


public void SetPlayerHealth()
{
    LocalPlayer.Vitals.SetHealth(50);
}

protected override void OnGameStart()
{
    SetPlayerHealth(); // calling the method above
}
            

In the example above, when we load into a game and gain player control, our OnGameStart method will automatically be executed as we have seen in the first mod tutorial.
This means that our SetPlayerHealth(); line will also be executed and the method will be called, changing the player health to 50.

Summing up, we can say we use methods to jump onto different sections of code, so we can keep it organized and understandable.

Passing parameters to methods

What does this mean? It means we can pass values (or variables) when calling our methods.
This makes them much more flexible as we can pass different values for each method.


public void SetHealth(int value)
{
    LocalPlayer.Vitals.SetHealth(value);
}

public void SetStamina(int value)
{
    LocalPlayer.Vitals.SetStamina(value);
}

protected override void OnGameStart()
{
    SetHealth(50);
    SetStamina(10);
}
            

In the above example we made a first method called SetHealth which takes an int parameter and another method called SetStamina which takes another int parameter.

When we boot into the game, our SetHealth method will be called first, passing the value 50 which will set the player health to it.
After that the SetStamina method will be called, passing the value 10 which will set the player stamina to it.

We can also pass multiple parameters in the same method as seen in the example below:


public void SetHealthAndStamina(int firstValue, int secondValue)
{
    LocalPlayer.Vitals.SetHealth(firstValue);
    LocalPlayer.Vitals.SetStamina(secondValue);
}

protected override void OnGameStart()
{
    SetHealthAndStamina(50, 10);
}
            

Methods returning a value

We have said before that methods can return a value of the defined type.
So we will take a method returning an int as an example below.


public int GetHealthValue()
{
    return LocalPlayer.Vitals.GetHealth();
}

protected override void OnGameStart()
{
    int returnedValue = SetHealth();
    LocalPlayer.Vitals.SetHealth(returnedValue);
}
            

When our OnGameStart method is executed, the GetHealthValue method is called and will return the current player health, which will be stored inside the returnedValue integer variable.
After that, the returnedValue int variable is used to set the player health to what the GetHealthValue method return before.

Summing up

As you have seen, methods are one fundamental of these mods, and you will need to use them 100% of the time.